package com.bicon.botu.mall.order.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.mall.order.common.OrderConstans;
import com.bicon.botu.mall.order.entity.MyInvoice;
import com.bicon.botu.mall.order.entity.OrderGoods;
import com.bicon.botu.mall.order.service.IMyInvoiceService;
import com.bicon.botu.mall.order.service.IOrderChildService;
import com.bicon.botu.mall.order.service.IOrderGoodsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 订单发票 后台控制器
 * @author dsp
 * @since 2018-05-14
 */
@RestController
@RequestMapping("/V3.0.0/my-invoice")
public class MyInvoiceControllerV3 extends BaseController {

    @Autowired
    private IMyInvoiceService iMyInvoiceService;

    @Autowired
    private IOrderChildService iOrderChildService;

    /**
     * 保存个人发票信息
     * @param myInvoice
     * @return
     */
    @RequestMapping(value="saveOrUpdate",method = RequestMethod.POST)
    public BaseResult update(MyInvoice myInvoice,String orderGoodsId){
        //必要参数校验
        if(myInvoice != null && StringUtils.isNoneBlank(myInvoice.getUserId(),orderGoodsId)
                && myInvoice.getType() != null){
            boolean result=iMyInvoiceService.saveOrUpdate(myInvoice,orderGoodsId);
            if(result){
                List<OrderGoods> list = new OrderGoods().selectList(new EntityWrapper<OrderGoods>()
                        .eq("logic_delete", Constants.DEL_LOGIC_NORMAL).eq("id",orderGoodsId));
                String remark = "修改发票信息";
                iOrderChildService.insertOrderRecord(getUserId(),list,remark);
                return sendResult200();
            }else {
                return sendResult(OrderConstans.RESPONSE_CODE_600,OrderConstans.RESPONSE_MESSAGE_600);
            }
        }else {
            return sendResult400();
        }
    }
}
